#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
using namespace std;


// Definition for singly-linked list.
  struct ListNode {
      int val;
      ListNode *next;
      ListNode() : val(0), next(nullptr) {}
      ListNode(int x) : val(x), next(nullptr) {}
      ListNode(int x, ListNode *next) : val(x), next(next) {}
  };
 
class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
       if(head==nullptr){
           return nullptr;
       }
       ListNode* pre = new ListNode(0);
       ListNode* rehead=nullptr;
       pre->next = head;
       ListNode* curr = head;
       int status = 0;
       while(curr!=nullptr){
           pair<bool,ListNode*> temp = func(curr);
           if(!temp.first){
               pre->next  = curr;
               pre = curr;
               if(status ==0){
                   rehead = curr;
                   status=1;
               }
               curr = curr->next;
           }
           else{
               
               curr = temp.second;
           }
       }
       pre->next = nullptr;
       return rehead;

    }
    pair<bool,ListNode*> func(ListNode* curr){
        if(curr->next==nullptr||curr->val!=curr->next->val){
            return make_pair(false,curr);
        }
        else{
            int temp = curr->val;
            while(curr&&curr->val==temp){
                curr = curr->next;
            }
            return make_pair(1,curr);
        }
    }
};

int main(){
    Solution S;
    ListNode* head = new ListNode(1);
    head->next = new ListNode(1);
    // head->next->next = new ListNode(1);
    // head->next->next->next =new ListNode(2);
    // head->next->next->next->next =new ListNode(3);
    // head->next->next->next->next->next =new ListNode(4);
    // head->next->next->next->next->next->next =new ListNode(5);
    ListNode* rehead =  S.deleteDuplicates(head);
    return 0;
}